//
//  StarZuopinScrollView.m
//  WULIHH
//
//  Created by tang on 16/4/22.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "StarZuopinScrollView.h"
#import "iCarousel.h"
#import "StarIconView.h"

#import "Header.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"

#import "WuLiRequestStarWorkAPI.h"



#import "SearchStarViewController.h"



@interface StarZuopinScrollView ()<iCarouselDataSource, iCarouselDelegate>
{
    
}



@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, assign) BOOL wrap;


@end


@implementation StarZuopinScrollView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _wrap = YES;
        self.items = [NSMutableArray array];
        
        [self initUI];
    }
    return self;
}





- (void)reloadWithArray:(NSMutableArray *)array
{
    [self.items removeAllObjects];
    [self.items addObjectsFromArray:array];
    [_carousel reloadData];
}




- (void)initUI
{
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
    backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //	backgroundView.image = [UIImage imageNamed:@"background.png"];
    [self addSubview:backgroundView];
    
    //create carousel
    _carousel = [[iCarousel alloc] initWithFrame:self.bounds];
    _carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _carousel.type = iCarouselTypeLinear;
    _carousel.delegate = self;
    _carousel.dataSource = self;
    
    //add carousel to view
    [self addSubview:_carousel];
    
}





- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//    NSLog(@"%d", _items.count);
    return [_items count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    //    UILabel *label = nil;
    
    
    UIImageView *imageView = nil;
    UILabel *nameLable = nil;
    UILabel *workCountLable = nil;
    
    
//    if (view == nil)
//    {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 65.0f, 95.0f)];
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 0, 0, 65, 65)];
        //        [imageView setBackgroundColor:[UIColor redColor]];
        [imageView.layer setCornerRadius:65.0 / 2.0];
        [imageView.layer setMasksToBounds:YES];
        [imageView setOpaque:NO];
        //    [topImageView setImage:image];
        [view addSubview:imageView];
        
        nameLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 68, 90, 45)];
        [nameLable setNumberOfLines:3.0];
        [nameLable setTextAlignment:NSTextAlignmentCenter];
        [nameLable setFont:[UIFont systemFontOfSize:12.0]];
        [view addSubview:nameLable];
        
        workCountLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(nameLable.frame)-40, 68, 90, 45)];
        [workCountLable setNumberOfLines:3.0];
        [workCountLable setTextAlignment:NSTextAlignmentCenter];
        [workCountLable setFont:[UIFont systemFontOfSize:12.0]];
        [view addSubview:workCountLable];

        view.contentMode = UIViewContentModeCenter;
//    }
    
//    if (self.items.count == 0)
//    {
//        [imageView setImage:[UIImage imageNamed:@"bigzengjia"]];
//    }
//    else
//    {
//        if (index == _items.count)
//        {
//            //            [nameLable setText:@"7"];
//            [imageView setImage:[UIImage imageNamed:@"bigzengjia"]];
//            //            NSLog(@"7");
//        }
//        else
//        {
            [nameLable setText:[NSString stringWithFormat:@"粉丝\n%@", [[self.items objectAtIndex:index] valueForKey:@"fansCount"]]];
            [workCountLable setText:[NSString stringWithFormat:@"作品\n%@", [[self.items objectAtIndex:index] valueForKey:@"workCount"]]];
            [imageView setImageWithURL:[NSURL URLWithString:[[self.items objectAtIndex:index] valueForKey:@"iconUrl"]]];
            //            NSLog(@"--------------%d", index);
            
//        }
//        
//    }
//    
    
    
    //    label.text = [_items[index] stringValue];
    
    return view;
}

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
}


- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    return 100.0f;
}


- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return _wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        default:
        {
            return value;
        }
    }
}



- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSString *string = [NSString stringWithFormat:@"%@", [[self.items objectAtIndex:index] valueForKey:@"id"]];
        
    [_MStarZuopinScrollViewDelegate requestStarWork:string];
}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
